<script setup>
import * as api from "@/api/v-msg-service/sms-manage/sms-send-log";
/**
 * 组件挂载后
 */
onMounted(() => {
  init()
})

/**
 * 列表数据
 */
const tableData = ref([])

/**
 * loading状态
 */
const loading = ref({
  tableLoading: false,// 列表loading
})

/**
 * 计算表格属性
 */
const tableInfo = computed(()=>{
  const tableProps = {
    data: tableData.value,
    loading: loading.value.tableLoading,
    rowConfig: { keyField: 'id' },
    checkboxConfig: { highlight: false, reserve: true },
    showSelectCount: true,
    showExportBtn: true,
  }
  return { ...tableProps }
})

/**
 * 分页参数
 */
const pageQuery = reactive({
  page: 1,
  limit: 50,
  total: 0,
  url: '/cloud-message-service/sms-send-log/page',
  orders: [
    {column: "sendTime", asc: false}
  ],
  filters: [],
  condition: {
    key: "",
  },
})

/**
 * 组件初始化调用函数
 */
const init = () => {
  init_PageQuery()
}

/**
 * 分页查询
 */
const init_PageQuery = async () => {
  loading.value.tableLoading = true
  const { code, data } = await api.page(pageQuery)
  if (code === 200) {
    loading.value.tableLoading = false
    tableData.value = data.records
    pageQuery.total = data.total
  }
}
/**
 * 删除
 */
const showRemoveDialog = ($event,row) => {
  ElMessageBox.confirm('', '确定删除吗',
      {
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '拼命加载中...'
            api.removeById(row.id).then(({code})=>{
              instance.confirmButtonLoading = false
              if (code === 200){
                ElMessage({message: '删除成功', type: 'success',})
                init_PageQuery()
              }
            })
            done()
          } else {
            done()
          }
        }
      }
  )
}

/**
 * 组件对外提供可访问数据
 */
defineExpose({
  showRemoveDialog,
})

</script>

<template>
  <div class="sms-send-box flex flex-col">
    <div class="btn">
      <div class="flex align-center">
        <label class="label">{{'\u2003'}}关键字{{'\u2003'}}</label>
        <el-input
            v-model="pageQuery.condition.key"
            style="width: 180px;" placeholder="请输入关键字"
            @keyup.enter="init_PageQuery" @clear="init_PageQuery" clearable
        />
        <div class="el-button" style="display: none"></div>
        <el-button type="primary" @click="init_PageQuery">
          <template #icon><i-ep-search /></template>搜索
        </el-button>
      </div>
    </div>
    <v-table
        v-model="pageQuery"
        :table-info="tableInfo"
        :column-info="api.columnInfo"
        @query="init_PageQuery"
    >
      <template #operate="{row}">
        <el-button type="primary" :text="true" @click="showRemoveDialog($event,row)">
          <template #icon><i-ep-delete /></template>删除
        </el-button>
      </template>
    </v-table>
  </div>
</template>

<style scoped lang="scss">
.sms-send-box {
  padding: 10px;
  .btn {
    text-align: right;
    position: absolute;
    top: -35px;
    right: 0;
    padding-right: 20px;
  }
  .container {
    .table {
      :deep(.el-tag) {
        cursor: pointer;
      }
      .el-button {
        padding: 0;
      }
    }
  }
}
</style>
